package com.diorsunion.hedge.io;

import com.diorsunion.hedge.bo.message.ServerMessageBO;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.function.Function;

/**
 * Created by harley-dog on 2016/5/18.
 */
@Component
public class ServerMessageHandler {

    @Resource
    ServerMessageBO serverMessageBO;

    /**
     * 消息处理类
     * @param message 接收到的消息
     * @param writeBack 要写回的消息
     */
    public void execute(Message message,Function<Message,Object> writeBack){
        //如果达到触发条件，立刻通知客户端交易
        //如果没有达到触发条件，则写进数据库
        List<Message> result = null;
        if(message.data==null){
            return;
        }
        result = serverMessageBO.execute(message);
        if(result!=null){
            result.stream().filter(x->x!=null).forEach(x->writeBack.apply(x));
        }
    }
}
